<!DOCTYPE html>
<html>

<head>
<title>jQuery-CSV Testing</title>
<meta charset="UTF-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="../src/jquery.csv.js"></script>
<script src="qunit.js"></script>
<link media="screen" href="qunit.css" rel="stylesheet"/>
<script>
$(document).ready(function() {

  // initialize the data for the 'core' module
  function coreDataInit() {
    toArray = '"You will come back stronger then ever","like Lance Armstrong","but with two balls"';
    fromArray = ["You will come back stronger then ever","like Lance Armstrong","but with two balls"];

    toArrays = '';
    toArrays += '"All work","and no play","makes Jack","a dull boy..."\n';
    toArrays += '"All work","and no play","makes Jack","a dull boy..."\n';
    toArrays += '"All work","and no play","makes Jack","a dull boy..."\n';
    toArrays += '"All work","and no play","makes Jack","a dull boy..."\n';
    toArrays += '"All work","and no play","makes Jack","a dull boy..."\n';
    toArrays += '"All work","and no play","makes Jack","a dull boy..."\n';
    toArrays += '"All work","and no play","makes Jack","a dull boy..."\n';
    toArrays += '"All work","and no play","makes Jack","a dull boy..."\n';
    toArrays += '"All work","and no play","makes Jack","a dull boy..."\n';
    toArrays += '"All work","and no play","makes Jack","a dull boy..."';

    fromArrays = [];
    fromArrays.push(["All work","and no play","makes Jack","a dull boy..."]);
    fromArrays.push(["All work","and no play","makes Jack","a dull boy..."]);
    fromArrays.push(["All work","and no play","makes Jack","a dull boy..."]);
    fromArrays.push(["All work","and no play","makes Jack","a dull boy..."]);
    fromArrays.push(["All work","and no play","makes Jack","a dull boy..."]);
    fromArrays.push(["All work","and no play","makes Jack","a dull boy..."]);
    fromArrays.push(["All work","and no play","makes Jack","a dull boy..."]);
    fromArrays.push(["All work","and no play","makes Jack","a dull boy..."]);
    fromArrays.push(["All work","and no play","makes Jack","a dull boy..."]);
    fromArrays.push(["All work","and no play","makes Jack","a dull boy..."]);

    toObjects = '"ID","iManufacturer","iMPartNumber","iSerialNumber","iSimCategory","iPartType","iDescription","iGroup","iLocation","iSold"\n';
    toObjects += '"1","Evans & Sutherland","230-132-111AA","","Visual","PCB","","1","Offsite",""\n';
    toObjects += '"2","Evans & Sutherland","230-132-111AA","","Visual","PCB","","1","Offsite",""\n';
    toObjects += '"3","Evans & Sutherland","230-120-112AC","","Visual","PCB","","1","Offsite",""\n';
    toObjects += '"4","Evans & Sutherland","230-120-112AC","","Visual","PCB","","1","Offsite",""\n';
    toObjects += '"5","Evans & Sutherland","230-120-112AC","","Visual","PCB","","1","Offsite",""\n';
    toObjects += '"6","Evans & Sutherland","230-120-112AC","","Visual","PCB","","1","Offsite",""\n';
    toObjects += '"7","Evans & Sutherland","230-120-112AC","","Visual","PCB","","1","Offsite",""\n';
    toObjects += '"8","Evans & Sutherland","230-120-112AC","","Visual","PCB","","1","Offsite",""\n';
    toObjects += '"9","Evans & Sutherland","230-120-112AC","","Visual","PCB","","1","Offsite",""\n';
    toObjects += '"10","Evans & Sutherland","230-121-150AC","","Visual","PCB","","1","Offsite",""';

    fromObjects = [];
    fromObjects.push({ID:"1",iManufacturer:"Evans & Sutherland",iMPartNumber:"230-132-111AA",iSerialNumber:"",iSimCategory:"Visual",iPartType:"PCB",iDescription:"",iGroup:"1",iLocation:"Offsite",iSold:""});
    fromObjects.push({ID:"2",iManufacturer:"Evans & Sutherland",iMPartNumber:"230-132-111AA",iSerialNumber:"",iSimCategory:"Visual",iPartType:"PCB",iDescription:"",iGroup:"1",iLocation:"Offsite",iSold:""});
    fromObjects.push({ID:"3",iManufacturer:"Evans & Sutherland",iMPartNumber:"230-120-112AC",iSerialNumber:"",iSimCategory:"Visual",iPartType:"PCB",iDescription:"",iGroup:"1",iLocation:"Offsite",iSold:""});
    fromObjects.push({ID:"4",iManufacturer:"Evans & Sutherland",iMPartNumber:"230-120-112AC",iSerialNumber:"",iSimCategory:"Visual",iPartType:"PCB",iDescription:"",iGroup:"1",iLocation:"Offsite",iSold:""});
    fromObjects.push({ID:"5",iManufacturer:"Evans & Sutherland",iMPartNumber:"230-120-112AC",iSerialNumber:"",iSimCategory:"Visual",iPartType:"PCB",iDescription:"",iGroup:"1",iLocation:"Offsite",iSold:""});
    fromObjects.push({ID:"6",iManufacturer:"Evans & Sutherland",iMPartNumber:"230-120-112AC",iSerialNumber:"",iSimCategory:"Visual",iPartType:"PCB",iDescription:"",iGroup:"1",iLocation:"Offsite",iSold:""});
    fromObjects.push({ID:"7",iManufacturer:"Evans & Sutherland",iMPartNumber:"230-120-112AC",iSerialNumber:"",iSimCategory:"Visual",iPartType:"PCB",iDescription:"",iGroup:"1",iLocation:"Offsite",iSold:""});
    fromObjects.push({ID:"8",iManufacturer:"Evans & Sutherland",iMPartNumber:"230-120-112AC",iSerialNumber:"",iSimCategory:"Visual",iPartType:"PCB",iDescription:"",iGroup:"1",iLocation:"Offsite",iSold:""});
    fromObjects.push({ID:"9",iManufacturer:"Evans & Sutherland",iMPartNumber:"230-120-112AC",iSerialNumber:"",iSimCategory:"Visual",iPartType:"PCB",iDescription:"",iGroup:"1",iLocation:"Offsite",iSold:""});
    fromObjects.push({ID:"10",iManufacturer:"Evans & Sutherland",iMPartNumber:"230-121-150AC",iSerialNumber:"",iSimCategory:"Visual",iPartType:"PCB",iDescription:"",iGroup:"1",iLocation:"Offsite",iSold:""});

    testEscaping = [];
    testEscaping.push('1,"he said ""you are crazy""", "one "", two "", three """');
    testEscaping.push('2,\'he said "\'you are crazy"\'\', \'one "\', two "\', three "\'\'');
    testEscaping.push('3;"he said ""you are crazy"""; "one "", two "", three """');
    testEscaping.push('4,"he said \\"you are crazy\\"", "one \\", two \\", three \\""');
    testEscaping.push('5|"he said ""you are crazy"""| "one "", two "", three """');
    resultEscaping = [];
    resultEscaping.push(['1', 'he said "you are crazy"', 'one ", two ", three "']);
    resultEscaping.push(['2', 'he said \'you are crazy\'', 'one \', two \', three \'']);
    resultEscaping.push(['3', 'he said "you are crazy"', 'one ", two ", three "']);
    resultEscaping.push(['4', 'he said "you are crazy"', 'one ", two ", three "']);
    resultEscaping.push(['5', 'he said "you are crazy"', 'one ", two ", three "']);

    testHooks = '';
    testHooks += '734,4.5,sda,"555","4523.35","af323"';
    resultHooks = [];
    resultHooks.push([734,4.5,"sda",555,4523.35,"af323"]);

    testNewLines = '';
    testNewLines += '"0","Evans & \nSutherland","230-132-111AA","","Visual","PCB"\n';
    testNewLines += '"0","Evans & \r\nSutherland","230-132-111AA","","Visual","PCB"\n';
    testNewLines += '"1","Evans & Sutherland","230-132-111AA","","Vis\nual","PCB"\r\n';
    testNewLines += '"1","Evans & Sutherland","230-132-111AA","","Vis\r\nual","PCB"\r\n';
    resultNewLines = [];
    resultNewLines.push(["0", "Evans & \nSutherland", "230-132-111AA", "", "Visual", "PCB"]);
    resultNewLines.push(["0", "Evans & \r\nSutherland", "230-132-111AA", "", "Visual", "PCB"]);
    resultNewLines.push(["1", "Evans & Sutherland", "230-132-111AA", "", "Vis\nual", "PCB"]);
    resultNewLines.push(["1", "Evans & Sutherland", "230-132-111AA", "", "Vis\r\nual", "PCB"]);

    testNullEntry = '';
    testNullEntry = '"Highsmith","Danson"\n'
    testNullEntry += '"You thinking what I\'m thinking partner?","aim for the bushes"\n';
    resultNullEntryArr = [];
    resultNullEntryArr.push(["Highsmith","Danson"]);
    resultNullEntryArr.push(["You thinking what I'm thinking partner?","aim for the bushes"]);
    resultNullEntryObj = [];
    resultNullEntryObj.push({Highsmith:"You thinking what I\'m thinking partner?",Danson:"aim for the bushes"});

    testNodeCallbacks = '';
    testNodeCallbacks += '"What are you","knittin\' a f*&%ing sweater"\n';
    testNodeCallbacks += '"I\'m trying to put some","tigers balm","on this jungle\'s nuts"';
    resultNodeCallbacks = [];
    resultNodeCallbacks.push(["What are you","knittin' a f*&%ing sweater"]);
    resultNodeCallbacks.push(["I'm trying to put some","tigers balm","on this jungle's nuts"]);
  }

  module("core", { setup:function(){ coreDataInit() }});

  test("Parsing - to Array(s)", function() {
    expect(2);
    stop();
    var output = $.csv.toArray(toArray);
    start();
    deepEqual(output, fromArray, "$csv.toArray()");
    stop();
    var output = $.csv.toArrays(toArrays);
    start();
    deepEqual(output, fromArrays, "$.csv.toArrays()");
    // TODO: Add tests
    //stop();
    //var output = $.fromArray(fromArray);
    //start();
    //deepEqual(output, toArray, "$.csv.fromArray()");
    //stop();
    //var output = $.fromArrays(fromArrays);
    //start();
    //deepEqual(output, toArrays, "$.csv.fromArrays()");
  });

  test("Parsing - to Objects", function() {
    expect(1);
    stop();
    var output = $.csv.toObjects(toObjects);
    start();
    deepEqual(output, fromObjects, "$.csv.toObjects()");
    // TODO: Add test
    //stop();
    //var output = $.fromObjects(fromObjects);
    //start();
    //deepEqual(output, fromObjects, "$.csv.fromObjects()");
  });

  test("Escaping - Custom Delimiter, Separator, and Escaper Characters", function() {
    expect(5);
    stop();
    var output = $.csv.toArray(testEscaping[0]);
    start();
    deepEqual(output, resultEscaping[0], "standard delimiter/separator/escaper");
    stop();
    var output = $.csv.toArray(testEscaping[1], {
      delimiter:"'"
    });
    start();
    deepEqual(output, resultEscaping[1], "custom delimiter");
    stop();
    var output = $.csv.toArray(testEscaping[2], {
      separator:';'
    });
    start();
    deepEqual(output, resultEscaping[2], "custom separator");
    stop();
    var output = $.csv.toArray(testEscaping[3], {
      escaper:'\\'
    });
    start();
    deepEqual(output, resultEscaping[3], "custom escaper");
    stop();
    var output = $.csv.toArray(testEscaping[4], {
      separator:'|'
    });
    start();
    deepEqual(output, resultEscaping[4], "regex escaping");
  });

  test("Hooks & User-Defined Callbacks", function() {
    expect(1);
    stop();
    var output = $.csv.toArrays(testHooks, {
      onParseValue: $.csv.hooks.castToScalar
    });
    start();
    deepEqual(output, resultHooks, "onParseValue hook - casting values to scalar types");
  });

  test("Edge Cases", function() {
    expect(3);
    stop();
    var output = $.csv.toArrays(testNewLines, {
      experimental:true
    });
    start();
    deepEqual(output, resultNewLines, "values containing newline chars");
    stop();
    var output = $.csv.toArrays(testNullEntry);
    start();
    deepEqual(output, resultNullEntryArr, "null last entry - toArrays()");
    stop();
    var output = $.csv.toObjects(testNullEntry);
    start();
    deepEqual(output, resultNullEntryObj, "null last entry - toObjects()");
  });

  test("Node.js", function() {
    expect(1);
    stop();
    $.csv.toArrays(testNodeCallbacks, {}, function(err, output) {
      deepEqual(output, resultNodeCallbacks, "Node.js-style callback support");
    });
    start();
  });
});
</script>
</head>

<body>
  <h1 id="qunit-header">jQuery-CSV Testing</h1>
  <h2 id="qunit-banner"></h2>
  <h2 id="qunit-userAgent"></h2>
  <ol id="qunit-tests">
  </ol>
</body>

</html>